package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.GpsDeviceDao;
import cn.topevery.garden.entity.po.GpsDevice;
import cn.topevery.garden.entity.query.GpsDeviceQuery;
import cn.topevery.garden.entity.vo.GpsDeviceVO;
import cn.topevery.garden.entity.vo.oneMap.CountVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


/**
 * 设备表
 *
 * @author code-generate
 * @date 2021-08-30
 */
@Repository
public class GpsDeviceDaoImpl extends BaseDaoImpl<GpsDevice> implements GpsDeviceDao {

    @Override
    public DataPager<GpsDeviceVO> listPage(GpsDeviceQuery query) {
        String table = "t_gps_device tgd";

        String columns = "tgd.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgd.c_id desc");
        sqlQueryMate
                .where(true, "tgd.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "tgd.c_code", ScriptConditionEnum.Like, query.getCode())
                .where(query.getDeviceType() != null, "tgd.c_device_type", ScriptConditionEnum.Eq, query.getDeviceType())
                .where(StringUtils.isNotBlank(query.getSimCode()), "tgd.c_sim_code", ScriptConditionEnum.Like, query.getSimCode())
                .where(query.getIdList() != null && query.getIdList().size()>0, "tgd.c_id", ScriptConditionEnum.NotIn, query.getIdList())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, GpsDeviceVO.class);
    }

    @Override
    public GpsDeviceVO getByCode(String code) {
        String table = "t_gps_device tgd";

        String columns = "tgd.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tgd.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tgd.c_code", ScriptConditionEnum.Eq, code);
        AtomicReference<GpsDeviceVO> gpsDevice = new AtomicReference<>(new GpsDeviceVO());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            gpsDevice.set(dbCommand.executeToEntity(GpsDeviceVO.class));
        });
        return gpsDevice.get();
    }

    /**
     * 描述 一张图管理 - 获取设施统计数量
     *
     * @return the type count
     * @author wenzhao.lin
     * @created 2021 -09-07 16:54:23
     */
    @Override
    public List<CountVO> getTypeCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("select count(1) value, c_device_type_name name from t_gps_device where c_db_status=0 group by c_device_type_name order by value desc");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    /**
     * 描述 一张图管理-运行监测-所有智能设备列表
     *
     * @return the all list data
     * @author wenzhao.lin
     * @created 2021 -09-10 15:27:17
     */
    @Override
    public List<GpsDeviceVO> getAllListData() {
        String table = "t_gps_device tgd left join t_device_object_relate dt on tgd.c_id = dt.c_device_id left join t_facilities f on f.c_id = dt.c_object_id ";
        String columns = "tgd.*,f.c_id c_object_id,f.c_name,f.c_lng_bd09 c_lng,f.c_lat_bd09 c_lat,f.c_geom";

        // 查询智能设备列表，c_device_type：0,-人，1-车
        List<Integer> idList = new ArrayList<>();
        idList.add(0);
        idList.add(1);

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgd.c_id desc");
        sqlQueryMate
                .where(true, "tgd.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tgd.c_device_type", ScriptConditionEnum.NotIn, idList)
                .where(true, "IFNULL(f.c_id,'y')", ScriptConditionEnum.NotEq, "y")
        ;
        return selectList(sqlQueryMate, GpsDeviceVO.class);
    }

}
